package br.com.async.application.impl;

import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import br.com.async.application.UserApplication;
import br.com.async.domain.login.User;
import br.com.async.domain.login.UserRepository;
import br.com.async.util.Encrypt;

@Transactional (propagation=Propagation.REQUIRED)
public class UserApplicationImpl implements UserApplication{
	private UserRepository userRepository;
	
	public UserApplicationImpl(UserRepository userRepository) {
		super();
		this.userRepository = userRepository;
	}

	@Override
	public void persist(User entity) {
		try {
			entity.setPassword(Encrypt.md5(entity.getUserName().trim() + entity.getPassword().trim()));
			userRepository.persist(entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void update(User entity) {
		userRepository.update(entity);
	}

	@Override
	public List<User> list() {
		return userRepository.list();
	}

	@Override
	public void disable(User entity) {
		userRepository.disable(entity);
	}

	@Override
	public User findByCode(Integer code) {
		return userRepository.findByCode(code);
	}

}
